package com.rjpacket.supernews.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.rjpacket.supernews.app.CommentActivity;
import com.rjpacket.supernews.app.R;
import com.rjpacket.supernews.app.model.Duanzi;

import java.util.List;

/**
 * Created by WIN7 on 2015/3/10.
 */

/**
 * 段子资源获取的数据适配器
 */

public class DuanziAdapter extends BaseAdapter implements View.OnClickListener {

    private Context context;
    private List<Duanzi> duanziList;
    private LayoutInflater inflater;
    private ViewHolder holder;

    private boolean isZan;
    private boolean isCai;
    private boolean isSaved;

    public DuanziAdapter(Context context, List<Duanzi> duanziList) {

        this.context = context;

        if (context != null) {
            inflater = LayoutInflater.from(context);
        } else {
            throw new IllegalArgumentException("Context must not be null");
        }
        this.duanziList = duanziList;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (duanziList != null) {
            ret = duanziList.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        Object ret = null;
        if (duanziList != null) {
            ret = duanziList.get(position);
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;

        if (convertView != null) {
            ret = convertView;
        } else {
            ret = inflater.inflate(R.layout.mz_item_fragment_duanzi, parent, false);
        }

        holder = (ViewHolder) ret.getTag();

        if (holder == null) {
            holder = new ViewHolder();

            //获得需要赋值的控件
            holder.zanNum = (TextView) ret.findViewById(R.id.fragment_duanzi_num_zan);
            holder.caiNum = (TextView) ret.findViewById(R.id.fragment_duanzi_num_cai);
            holder.commentNum = (TextView) ret.findViewById(R.id.fragment_duanzi_num_comment);
            holder.itemContent = (TextView) ret.findViewById(R.id.item_fragment_duanzi_content);

            //获得需要点击的控件
            holder.cai = (ImageView) ret.findViewById(R.id.fragment_duanzi_img_cai);
            holder.zan = (ImageView) ret.findViewById(R.id.fragment_duanzi_img_zan);
            holder.comment = (ImageView) ret.findViewById(R.id.fragment_duanzi_img_comment);
            holder.save = (ImageView) ret.findViewById(R.id.fragment_duanzi_img_save);
            holder.transfer = (ImageView) ret.findViewById(R.id.fragment_duanzi_img_transfer);

            //设置标签
            ret.setTag(holder);
        }

        Duanzi duanzi = duanziList.get(position);

        //设置踩的个数
        String bad_num = duanzi.getBad_num() + "";
        holder.caiNum.setText(bad_num);

        //设置赞的个数
        String good_num = duanzi.getGood_num() + "";
        holder.zanNum.setText(good_num);

        //设置评论的个数
        String comment_num = duanzi.getComment_num() + "";
        holder.commentNum.setText(comment_num);

        //设置内容
        String content = duanzi.getContent();
        holder.itemContent.setText(content);

        //绑定点击事件
        holder.save.setOnClickListener(this);
        holder.zan.setOnClickListener(this);
        holder.comment.setOnClickListener(this);
        holder.transfer.setOnClickListener(this);
        holder.cai.setOnClickListener(this);

        return ret;
    }

    //点击选项中的图标的事件
    @Override
    public void onClick(View v) {
        int id = 0;
        if (v != null) {
            id = v.getId();
        }
        switch (id) {
            //点击踩
            case R.id.fragment_duanzi_img_cai:
                //TODO 制作动画 +1

                if (!isCai) {
                    if (!isZan) {
                        String num = holder.caiNum.getText().toString();
                        int caiNum = Integer.parseInt(num);
                        caiNum++;
                        holder.zanNum.setText(caiNum + "");
                        holder.zan.setImageResource(R.drawable.ic_jokes_bad_enabled);
                        notifyDataSetChanged();
                        isCai = true;
                    }
                } else {
                    Toast.makeText(context, "您已经踩过", Toast.LENGTH_SHORT).show();
                }
                break;
            //点击赞
            case R.id.fragment_duanzi_img_zan:
                //TODO 制作动画 +1

                if (!isZan) {
                    if (!isCai) {
                        String good_num = holder.zanNum.getText().toString();
                        int zanNum = Integer.parseInt(good_num);
                        zanNum++;
                        holder.zanNum.setText(zanNum + "");
                        holder.zan.setImageResource(R.drawable.ic_have_good);
                        notifyDataSetChanged();
                        isZan = true;
                    }
                } else {
                    Toast.makeText(context, "您已经赞过", Toast.LENGTH_SHORT).show();
                }
                break;
            //点击收藏
            case R.id.fragment_duanzi_img_save:
                //TODO 保存到我的收藏
                if (!isSaved) {
                    Toast.makeText(context, "收藏成功", Toast.LENGTH_SHORT).show();
                    holder.save.setImageResource(R.drawable.ic_newsbar_collected);
                    notifyDataSetChanged();
                } else {
                    Toast.makeText(context, "取消收藏", Toast.LENGTH_SHORT).show();
                    holder.save.setImageResource(R.drawable.ic_save);
                    notifyDataSetChanged();
                }
                break;
            //点击分享
            case R.id.fragment_duanzi_img_transfer:
                //TODO 分享
                break;
            //点击评论
            case R.id.fragment_duanzi_img_comment:
                Intent intent = new Intent(context, CommentActivity.class);
                context.startActivity(intent);
                break;
        }
    }

    private class ViewHolder {
        //段子内容
        public TextView itemContent;
        //点赞个数
        public TextView zanNum;
        //踩的个数
        public TextView caiNum;
        //评论个数
        public TextView commentNum;

        // 各种需要点击的ImageView
        public ImageView zan;
        public ImageView cai;
        public ImageView comment;
        public ImageView save;
        public ImageView transfer;

    }
}
